using Digitalmes.Domain.Entities.Mes.Productions;

namespace Digitalmes.Application.Mes.Productions.WorkOrders.Queries;

/// <summary>
/// 排程工单查询命令。
/// </summary>
/// <param name="LineId"></param>
public sealed record GetScheduleWorkOrderListQuery(long LineId) : IRequest<IResult<List<ProdWorkOrderDto>>>;

internal sealed class GetScheduleWorkOrderListQueryHandler(IRepository<ProdWorkOrder> repo) : IRequestHandler<GetScheduleWorkOrderListQuery, IResult<List<ProdWorkOrderDto>>>
{
    public async Task<IResult<List<ProdWorkOrderDto>>> Handle(GetScheduleWorkOrderListQuery request, CancellationToken cancellationToken)
    {
        WoStatusEnum[] statuns = [WoStatusEnum.Issued, WoStatusEnum.Scheduled, WoStatusEnum.InProgress, WoStatusEnum.OnHold];
        var items = await repo.AsQueryable()
            .Includes(m => m.Product)
            .Where(m => m.LineId == request.LineId && statuns.Contains(m.Status))
            .OrderBy(m => m.Priority)
            .ToListAsync(cancellationToken);
        return Result.From(items.Adapt<List<ProdWorkOrderDto>>());
    }
}
